﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Common.Command;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;
using ZLCMS.Business.Common;
using Newtonsoft.Json;

namespace ZLCMS.Web.ZLOAuth.Code
{
    /// <summary>
    /// GetMemberAccount命令参数
    /// </summary>
    public class GetMemberTokenParameter : CommandParameter
    {
        public string AuthCode { get; set; }

        public string UserCode { get; set; }
    }

    /// <summary>
    /// GetMemberAccount命令调用输出
    /// </summary>
    public class GetMemberTokenResult
    {
        public string Token { get; set; }

        public string Name { get; set; }

        public string Photo { get; set; }

        public string Sex { get; set; }
    }

    /// <summary>
    /// GetMemberAccount命令
    /// </summary>
    public class GetMemberTokenCommand : SessionCommand<GetMemberTokenResult>
    {
        public override ICommandResult<GetMemberTokenResult> Execute(NHibernate.ISession session, ICommandParameter parameter)
        {
            ICommandResult<GetMemberTokenResult> result = new CommandResult<GetMemberTokenResult>();
            GetMemberTokenParameter param = parameter as GetMemberTokenParameter;
            if (param != null)
            {
                //在这里添加命令内容
                result.Result.Token = session.CreateSQLQuery("select member_account from member_oauth_code where user_code=:c and oauth_code=:o and status='0'")
                    .SetParameter("c", param.UserCode)
                    .SetParameter("o", param.AuthCode)
                    .UniqueResult<string>();
                object[] datas = session.CreateSQLQuery("select member_name,photo_url,sex from member_info where account_id=:acc")
                    .SetParameter("acc", result.Result.Token)
                    .UniqueResult<object[]>();
                if (datas != null)
                {
                    result.Result.Name = datas[0] as string;
                    try
                    {
                        List<SimpleFileInfo> fileDb = FileKit.FileList(datas[1] as string);
                        if (fileDb.Count == 1)
                        {
                            if (fileDb[0].FilePath.StartsWith("http"))
                            {
                                fileDb[0].FilePath = fileDb[0].FilePath;
                            }
                            else
                            {
                                fileDb[0].FilePath = ServerUtil.GetRootURI() + fileDb[0].FilePath;
                            }
                        }
                        result.Result.Photo = JsonConvert.SerializeObject(fileDb);
                    }
                    catch
                    {
                    }
                    result.Result.Sex = datas[2] as string;

                    //设置为已使用
                    session.CreateSQLQuery("update member_oauth_code set status='1' where user_code=:c and oauth_code=:o")
                    .SetParameter("c", param.UserCode)
                    .SetParameter("o", param.AuthCode)
                    .ExecuteUpdate();
                }
            }
            else
            {
                result.ErrorCode = "0001";
            }
            return result;
        }
    }
}
